package moonlit.chill.pay.service.impl.ali;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import lombok.extern.slf4j.Slf4j;
import moonlit.chill.pay.cache.PayConfigDataCache;
import moonlit.chill.pay.constants.Constants;
import moonlit.chill.pay.service.PayStrategy;
import moonlit.chill.pay.util.AliPayClientUtil;
import moonlit.chill.pay.vo.PayConfig;
import moonlit.chill.pay.vo.PayResult;
import moonlit.chill.pay.vo.RefundResult;
import moonlit.chill.pay.vo.TradeParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MoonlitChill
 * @date 2024/4/15 18:50
 */
@Slf4j
@Service("aliRefundQueryPayStrategy")
public class AliRefundQueryStrategyServiceImpl implements PayStrategy {
    @Resource
    private PayConfigDataCache payConfigDataCache;

    @Autowired
    private AliPayClientUtil aliPayClientUtil;

    @Override
    public PayResult<?> payForRefundQuery(TradeParam tradeParam) {
        log.info(String.format("支付宝退款查询参数:%s", JSONUtil.toJsonStr(tradeParam)));
        PayResult<String> result = new PayResult<>();
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
        model.setOutRequestNo(tradeParam.getTransNum());
        model.setOutTradeNo(tradeParam.getRefundNo());
        List<String> list = new ArrayList<>();
        list.add("gmt_refund_pay");
        model.setQueryOptions(list);
        request.setBizModel(model);
        try {
            PayConfig payConfig = payConfigDataCache.getPayConfig();
            AlipayClient alipayClient = aliPayClientUtil.getAliPayClient(payConfig);
            //证书模式
            AlipayTradeFastpayRefundQueryResponse response;
            if (payConfig.getAliType() == 0){
                //证书模式
                response = alipayClient.certificateExecute(request);
            } else {
                //密钥模式
                response = alipayClient.execute(request);
            }
            log.info(String.format("支付宝退款查询参数:%s  返回数据:%s", JSONUtil.toJsonStr(tradeParam), JSONUtil.toJsonStr(response)));
            if (response.isSuccess()) {
                RefundResult refundResult = new RefundResult();
                refundResult.setOutTradeNo(response.getTradeNo());
                refundResult.setTransNum(response.getOutTradeNo());
                refundResult.setRefundNo(response.getOutRequestNo());
                refundResult.setRefundFee(new BigDecimal(response.getRefundAmount()));
                refundResult.setRefundStatus(response.getRefundStatus());
                refundResult.setRefundTime(DateUtil.format(response.getGmtRefundPay(), DatePattern.NORM_DATETIME_PATTERN));
                result.setSuccess(Boolean.TRUE);
                result.setResult(JSONUtil.toJsonStr(refundResult));
            } else {
                result.error(response.getSubMsg(), Constants.PAY_FAIL_CODE);
            }
        } catch (AlipayApiException e) {
            result.errorWithException(e.getErrMsg(), Constants.PAY_ERROR_CODE, e);
        }
        return result;
    }
}
